﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;

namespace SplendidCRM.Ext.Models
{
    public class DynamicDataModel : DynamicObject, IDictionary<String, Object>
    {
        public DynamicDataModel()
            : this(StringComparer.OrdinalIgnoreCase)
        {
        }

        public DynamicDataModel(IDictionary<String, Object> properties)
            : this(properties, StringComparer.OrdinalIgnoreCase)
        {
        }

        public DynamicDataModel(IDictionary<String, Object> properties, IEqualityComparer<String> comparer)
        {
            if (comparer == null) throw new ArgumentNullException("comparer");
            if (properties != null) _properties = new Dictionary<String, Object>(properties, comparer);
        }

        public DynamicDataModel(IEqualityComparer<string> comparer)
        {
            if (comparer == null) throw new ArgumentNullException("comparer");
            _properties = new Dictionary<String, Object>(comparer);
        }

        private readonly IDictionary<String, Object> _properties;

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            _properties.TryGetValue(binder.Name, out result);
            return true;
        }

        public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
        {
            var index = indexes.Length == 0 ? String.Empty : indexes[0] == null ? String.Empty : indexes[0].ToString();
            _properties.TryGetValue(index, out result);
            return true;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            _properties[binder.Name] = value;
            return true;
        }

        public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value)
        {
            var index = indexes.Length == 0 ? String.Empty : indexes[0] == null ? String.Empty : indexes[0].ToString();
            _properties[index] = value;
            return true;
        }

        #region Implementation of IEnumerable

        public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
        {
            return _properties.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        #region Implementation of ICollection<KeyValuePair<string,object>>

        public void Add(KeyValuePair<string, object> item)
        {
            _properties.Add(item);
        }

        public void Clear()
        {
            _properties.Clear();
        }

        public bool Contains(KeyValuePair<string, object> item)
        {
            return _properties.Contains(item);
        }

        public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
        {
            _properties.CopyTo(array, arrayIndex);
        }

        public bool Remove(KeyValuePair<string, object> item)
        {
            return _properties.Remove(item);
        }

        public int Count
        {
            get { return _properties.Count; }
        }

        public bool IsReadOnly
        {
            get { return _properties.IsReadOnly; }
        }

        #endregion

        #region Implementation of IDictionary<string,object>

        public bool ContainsKey(string key)
        {
            return _properties.ContainsKey(key);
        }

        public void Add(string key, object value)
        {
            _properties.Add(key, value);
        }

        public bool Remove(string key)
        {
            return _properties.Remove(key);
        }

        public bool TryGetValue(string key, out object value)
        {
            return _properties.TryGetValue(key, out value);
        }

        public object this[string key]
        {
            get { return _properties[key]; }
            set { _properties[key] = value; }
        }

        public ICollection<string> Keys
        {
            get { return _properties.Keys; }
        }

        public ICollection<object> Values
        {
            get { return _properties.Values; }
        }

        #endregion
    }
}